package com.tl.gateway;

import jade.wrapper.gateway.JadeGateway;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 发送消息行为
 *
 * @author: TangLiang
 * @date: 2020/6/4 9:30
 * @since: 1.0
 */
public class SendMsgAction implements Action {
    @Override
    public void perform(HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String msg = req.getParameter("param1");
        DataChannel dataChannel = new DataChannel(msg, "receiver");
        try {
            JadeGateway.execute(dataChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.print("Message has been sent!<br/>");
        out.print("Reply:" + dataChannel.getMessage());
        out.print("<br/><a href='index.jsp'> Go back </a>");
        out.flush();
        out.close();
    }
}
